package com.musiklegal;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;



public class FBParser extends DefaultHandler 
{
	
	MerchantEntity aMerchant;
	boolean fName=false,uName=false,fImg=false,stat=false,entry =false;
	StringBuffer buffer;
	Fbdata data;

	FBParser()
	{
		buffer= new StringBuffer();
		
	}
	
	
	public void startDocument() throws SAXException
	{
	}
	
	public void endDocument() throws SAXException
	{
		System.gc();	
	}

	public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException
	{
		if(localName.equals("entry"))
		{
			entry=true;
			data = new Fbdata();
		}
		else if(localName.equals("fullname"))
			fName=true;
		else if(localName.equals("username"))
			uName=true;
		else if(localName.equals("picture"))
			fImg=true;
		else if(localName.equals("status"))
			stat=true;
		
		
	}
	
	
	public void endElement(String namespaceURI, String localName, String qName)
	throws SAXException 
	{
		
		if(localName.compareToIgnoreCase("entry")==0&&entry)
		{
			Friend.fbd.add(data);
			data=null;
			entry = false;
		}
		else if(localName.equals("fullname")&&entry)
			{
			data.name = buffer.toString();
			buffer = new StringBuffer();
				fName=false;
			}
		else if(localName.equals("username")&&entry)
			{
			data.use = buffer.toString();
			buffer = new StringBuffer();
				uName=false;
			}
		else if(localName.equals("picture")&&entry)
			{
			data.pict = buffer.toString();
			buffer = new StringBuffer();
				fImg=false;
			}
		else if(localName.equals("status")&&entry)
			data.status = buffer.toString();
			buffer = new StringBuffer();
			stat=false;
	}
	
	 public void characters(char ch[], int start, int length) 
	 {
		 
		 if(fName&&entry)
		 {
			 buffer.append(new String(ch,start,length));
		 }
		 else if(uName&&entry)
		 {
			 buffer.append(new String(ch,start,length));
		 }
		 else if(fImg&&entry)
		 {
			 buffer.append(new String(ch,start,length));
		 }
		 else if(stat&&entry)
		 {
			 buffer.append(new String(ch,start,length));
		 }
	 }
}